appCtrl.controller('MineCtrl', function($scope, $rootScope, $state, UtilsService) {

  })
  .controller('MineCreditCtrl', function($scope) {})

.controller('MineSettingCtrl1', function($scope, $rootScope, UtilsService) {
  // console.log(111111111);
  $scope.clearCookie = function() {
    // console.log(111111111);
    $rootScope.$broadcast('loading:showMessage', {
      message: '清理缓存成功'
    });
  }
})

.controller('MineCommentCtrl', function($scope, $rootScope, $state, UtilsService, UserService) {
  $scope.addFeedback = function(content) {
    // console.log(content);
    if(UtilsService.isLogin()){
      UserService.addFeedback({
        content: content
      }).$promise.then(function(response) {
        if (UtilsService.isResponseOk(response)) {
          $state.go('dash.mine');
        }
      })
    }
  }
})





.controller('MineAboutusCtrl', function($scope) {})



.controller('MineInfoCtrl', function($scope, $rootScope, $state, UtilsService) {
  console.log(UtilsService.getUser());

  var _this = $scope;

  $rootScope.$on('user:update', function(event, args) {
    _this.user = args.user;
  })

  $scope.user = UtilsService.getUser();

  $scope.upload = function(url) {
    if(!$scope.user){
      return;
    }
    console.log("开始上传：");
    var wt = plus.nativeUI.showWaiting();
    var task = plus.uploader.createUpload(baseUrl + '/user/headimg', {
        method: "POST"
      },
      function(t, status) { //上传完成
        wt.close();
        if (status == 200) {
          console.log("上传成功：" + t.responseText);
          var response = angular.fromJson(t.responseText);
          if (UtilsService.isResponseOk(response)) {
            $scope.user.headimg = url;
            UtilsService.setUser($scope.user);
          } else {
            $rootScope.$broadcast('loading:showMessage', {
              message: '提交失败'
            });
          }
        }
      }
    );

    task.addFile(url, {
      key: 0
    });
    task.start();
  }

  $scope.changeHead = function() {
    //  	outSet( "弹出系统选择按钮框：" );
    var bts = [{
      title: "拍照上传"
    }, {
      title: "相册中选择"
    }];
    plus.nativeUI.actionSheet({
        title: "修改头像",
        cancel: "取消",
        buttons: bts
      },

      function(e) {
        switch (e.index) {
          case 1:
            // 拍照上传
            console.log("开始拍照：");
            var cmr = plus.camera.getCamera();
            cmr.captureImage(function(p) {
              console.log("成功：" + p);
              plus.io.resolveLocalFileSystemURL(p, function(entry) {
                $scope.upload(entry.toLocalURL());
              }, function(e) {
                console.log("读取拍照文件错误：" + e.message);
              });
            }, function(e) {
              console.log("失败：" + e.message);
            }, {
              filename: "_doc/camera/",
              index: 1
            });
            break;
          case 2:
            // 系统相册
            plus.gallery.pick(function(path) {
              plus.io.resolveLocalFileSystemURL(path, function(entry) {
                $scope.upload(entry.toLocalURL());
              }, function(e) {
                console.log("读取文件错误：" + e.message);
              });
            }, function(e) {
              console.log("取消选择图片");
            }, {
              filter: "image"
            });
            break;
        }
      }
    );
  }

  $scope.logout = function() {
    UtilsService.logout();
  }
})



.controller('MinePasswordCtrl', function($scope, $rootScope, $state, UtilsService, UserService) {
  $scope.changePwd = function() {
    // console.log(this.oldpwd);
    UserService.changePwd({
      oldpwd: this.oldpwd,
      newpwd: this.newpwd
    }).$promise.then(function(response) {
      if (UtilsService.isResponseOk(response)) {
        $state.go('mine.info');
      }
    })
  }
})



.controller('MineSettingCtrl', function($scope) {})



.controller('MineAddressCtrl', function($scope, $rootScope, $state, UtilsService, UserService, AddressService) {
  if (!UtilsService.isLogin()) {
    $state.go('login');
    return;
  }
})



.controller('MineAddressAddCtrl', function($scope, $rootScope, $state, UtilsService, UserService, AddressService) {
  $scope.dist = UtilsService.getDist();

  // 添加地址
  $scope.addAddr = function() {
    console.log($scope.dist);
    this.address.district_id = $scope.dist.district_id;
    AddressService.save(this.address).$promise.then(function(response) {
      if (UtilsService.isResponseOk(response)) {
        $state.transitionTo('mine.address.list', {}, {
          reload: true,
          inherit: false,
          notify: true
        });
      }
    })
  }

})



.controller('MineAddressListCtrl', function($scope, $rootScope, $state, $ionicHistory, UtilsService, UserService, AddressService, RegionService) {

    $scope.addresses = [];

    // 地址列表
    AddressService.get().$promise.then(function(response) {
      if (UtilsService.isResponseOk(response)) {
        $scope.addresses = response.data.addresses;
      }
    })

    // 设置默认地址
    $scope.setDefault = function(addr) {
      AddressService.setDefault({
        address_id: addr.address_id
      }).$promise.then(function(response) {
        if (UtilsService.isResponseOk(response)) {
          for (var i in $scope.addresses) {
            $scope.addresses[i].default = 0;
          }
          addr.default = 1;
        }
      })
    }



    // 刪除地址
    $scope.deleteAddr = function(address) {
      AddressService.delete({
        id: address.address_id
      }).$promise.then(function(response) {
        if (UtilsService.isResponseOk(response)) {
          var i = $scope.addresses.indexOf(address);
          $scope.addresses.splice(i, 1);
        }
      })
    }

    // 选中地址
    console.log($rootScope.prevState);
    $scope.selectLoc = function(loc) {
      // if()
      $scope.prevState = $rootScope.prevState;
      if ($scope.prevState == 'order.chooseloc') {
        $rootScope.loc = loc;
        // $state.
        $ionicHistory.goBack();
        $rootScope.prevState = undefined;
      }
    }

    $scope.goBack = function() {
      UtilsService.goBack();
    }
  })
  .controller('MineNameCtrl', function($scope, $rootScope, $state, UtilsService, UserService) {
    $scope.user = UtilsService.getUser();
    // console.log( UtilsService.getUser());
    var _this = $scope;
    $scope.setName = function() {
      if (this.user.name == this.name) {
        return;
      }

      console.log(this.user);
      console.log(this.name);

      var __this = this;
      UserService.setName({
        name: this.name
      }).$promise.then(function(response) {
        if (UtilsService.isResponseOk(response)) {
          __this.user.name = __this.name;
          UtilsService.setUser(__this.user);
          $rootScope.$broadcast('user:update', {
            user: __this.user
          });
          $state.go('mine.info');
        }
      });
    }
  });
